--
-- Rotor module interface definition
--

--
-- Local refrences to global objects
--
local Rotor = _G['Rotor']
local CreateFrame = _G['CreateFrame']

--
-- Create new Rotor plugin with default impelmentation of interface functions
--
local function newPluginInstance(plugin_name) return {
    --
    -- Answer plugin name
    --
    -- arguments:
    --      none
    --  returns:
    --      module name <string>
    --
    Name = function(self)
        return plugin_name
    end,

    --
    -- Initialize plugin
    --
    -- arguments:
    --      options<table> - table of module options (persist between logons if Rotor:rememberPluginOptions(plugin, options) is called)
    -- return: none
    --
    Init = function(self, options)
        -- do nothing by default
    end,

    --
    -- Update function. Called when frame OnUpdate event occured.
    --
    -- arguments:
    --      elapsed<float> - time in seconds since last call to Update
    -- return:
    --      none
    --
    Update = function(self, elapsed) end,

    --
    -- called when graphical scale changed
    ScaleChanged = function(self, new_scale) end,

    --
    -- called when graphical alpha changed
    --
    AlphaChanged = function(self, new_alpha) end,

    --
    -- Called when spells auras and resources fully updated
    --
    ResourcesAndCooldownsUpdated = function(self) end,

    --
    -- Called when rotor shown/hidden.
    --
    MainInterfaceVisibilityChanged = function(self, main_interface_visible) end,

    CurrentModuleFinalized = function(self) end,

    NewModuleSelected = function(self) end,

    --
    -- Notify module about target changed.
    --
    -- arguments:
    --      old_target_guid - GUID of previous valid target
    -- return:
    --      none
    TargetChanged = function(self, old_target_guid) end,

    --
    -- Notify module when unit is entering or leaving combat
    --
    -- arugments:
    --      none
    -- return:
    --      none
    --
    CombatStateChanged = function(self) end,

    --
    -- Handle commands from command line. called when /rr opt ... is executed
    --
    -- arguments:
    --      cmd_line<string> - text after '/rr opt'
    -- return:
    --      true - module reinitialization required, false otherwise
    --
    HandleCommandLine = function(self, cmd_line)
        Rotor:Print('plugin "'..plugin_name..'" does not have options')
        return false
    end,

    --
    -- events support
    --
    event_frame = nil,

    registerEvent = function(self, event)
        if not self.event_frame then
            self.event_frame = CreateFrame('Frame')
            self.event_frame:SetScript('OnEvent', function(this, event, ...) self[event](self, ...) end)
        end
        self.event_frame:RegisterEvent(event)
    end,

    unregisterEvent = function(self, event)
        if self.event_frame then
            self.event_frame:UnregisterEvent(event)
        end
    end,
}
end

----------------------------------------
--
-- Create and register new plugin
--
function Rotor:NewPlugin(plugin_name)
    if self.plugins and self.plugins[plugin_name] then
        error('plugin "'..plugin_name..'" alredy exists')
    end

    local plugin = newPluginInstance(plugin_name)

    if not self.plugins then
        self.plugins = {}
    end
    self.plugins[plugin_name] = plugin
    return plugin
end
----------------------------------------
function Rotor:savePluginOptions(plugin, opts)
    if not self.cfg.plugin_opts then
        self.cfg.plugin_opts = {}
    end
    self.cfg.plugin_opts[plugin:Name()] = opts
end
